/*
 * main.c
 *
 * Created: 12/1/2013 4:01:04 PM
 *  Author: Mishaal
 */ 

#include "adc_drv.h"
#include "avr_can_board.h"
//#include "lcd.h"
#include "avr/io.h"
//#include "LCD_new.h"
#include "lcd_lib.h"
#include "can_lib.h"
#include "stdlib.h"
#include <util/delay.h>
#define	__AVR_AT90CAN128__	1
#define OSCSPEED	16000000	

void display_sensor_values(void);
void WDT_Off(void);

int main()
{
	//PORTA = 0b00000000;
	DDRA = 0b11111111;

	PORTB = 0b00000000;
	DDRB = 0b00000000;

	PORTC = 0b00000000;
	DDRC = 0b00000000;

	PORTD = 0b00000000;
	DDRD = 0b00000000;
	
	PORTF = 0b00000000;
	DDRF = 0b00000000;

	
	DDRE = 0b00010000;
	//DDRE |= 0b00010000;		//Led set as output (Bit4 = 1) 
	//DDRE &= 0b11011111;		//Button set as input(Bit 5 = 0)
	PORTE = 0b00110000;
	//PORTE |= 0b00010000;
	WDT_Off();
	
	LCDinit();
	LCDclr();
	LCDhome();
	LCDGotoXY(0,0);
	LCDsendChar('M');
	can_init(0);

	display_sensor_values();
	
	return 0;
}

void WDT_Off(void)
{
	WDTCR = (1<<WDCE) | (1<<WDE);
	WDTCR = 0x07;				//increase Watchdog time-out period (Bit0,1,2 = 1)
}

void display_sensor_values(void)
{
	U8 i; 
	unsigned int remote_frame_count=0, data_frame_count=0;
	char string[10];
	U8 tx_remote_buffer[8];
	st_cmd_t tx_remote_msg;

	U8 response_buffer[8];
	st_cmd_t response_msg;

	// --- Init variables
	tx_remote_msg.pt_data = &tx_remote_buffer[0];
	tx_remote_msg.status = 0;

	response_msg.pt_data = &response_buffer[0];
	response_msg.status = 0;

	while (1)
	{
		_delay_ms(2000);  // 2 secondes between refreshed screen
		
			// --- Init Tx Commands
			for(i=0; i<8; i++) tx_remote_buffer[i]=0;
			tx_remote_msg.id.std = 10 + 1;
			tx_remote_msg.ctrl.ide = 0;
			tx_remote_msg.ctrl.rtr = 1;
			tx_remote_msg.dlc = 4;
			tx_remote_msg.cmd = CMD_TX_REMOTE;
			// --- Tx Command
			while(can_cmd(&tx_remote_msg) != CAN_CMD_ACCEPTED);

			// --- Wait for Tx remote completed
			while(can_get_status(&tx_remote_msg) == CAN_STATUS_NOT_COMPLETED)

			_delay_ms(50); // Wait 50ms for a response if exits
			if (can_get_status(&response_msg) == CAN_STATUS_COMPLETED)
			{
				remote_frame_count++;
				itoa(remote_frame_count,string,6);
				LCDclr();
				LCDstring("Remote frame ", 13);
				LCDstring(string, 1);
			}
			else
			{
				response_msg.cmd = CMD_ABORT;
				while (can_cmd(&response_msg) != CAN_CMD_ACCEPTED);
				LCDstring("Remote fail", 11);
			}
			
			// --- Init Tx Commands
			for(i=0; i<8; i++) tx_remote_buffer[i]=100*i;
			tx_remote_msg.id.std = 10 + 2;
			tx_remote_msg.ctrl.ide = 0;
			tx_remote_msg.ctrl.rtr = 0;
			tx_remote_msg.dlc = 4;
			tx_remote_msg.cmd = CMD_TX_REMOTE;
			// --- Tx Command
			while(can_cmd(&tx_remote_msg) != CAN_CMD_ACCEPTED);

			// --- Wait for Tx remote completed
			while(can_get_status(&tx_remote_msg) == CAN_STATUS_NOT_COMPLETED)

			_delay_ms(50); // Wait 50ms for a response if exits
			if (can_get_status(&response_msg) == CAN_STATUS_COMPLETED)
			{
				data_frame_count++;
				itoa(data_frame_count, string, 6);
				LCDclr();
				LCDstring("data frame ", 11);
				LCDstring(string, 1); 
			}
			else
			{
				response_msg.cmd = CMD_ABORT;
				while (can_cmd(&response_msg) != CAN_CMD_ACCEPTED);
				LCDstring("data fail", 9);
			}		
	}
}